package jp.co.sega.sonic_jump.monthly.sum;

import android.content.res.Resources;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.InputStream;
import jp.co.sega.sonic_jump.monthly.sum.R;
import java.io.IOException;

public class ResourceMaster {
    private static int currentPak = -1;
    private static DataInputStream dis;
    private static int disPosition;
    private static int[] fileLength;
    private static int[] fileOffset;
    private static Resources m_res;

    public static void setResources(Resources res) {
        m_res = res;
    }

    public static byte[] getResource(int id) {
        if (id == -1) {
            return null;
        }
        int fileID = id & 65535;
        int pakID = (id >> 16) & 65535;
        byte[] returnBytes = null;
        if (pakID == 0) {
            try {
                returnBytes = getResourceFromJar("res" + fileID, true);
            } catch (Exception e) {
            }
        } else {
            if (currentPak != pakID) {
                initPak(pakID);
            }
            int offset = fileOffset[fileID];
            int length = fileLength[fileID];
            try {
            if (dis == null || disPosition > offset) {
                dis.close();
                dis = null;
                dis = new DataInputStream(m_res.openRawResource(R.drawable.class.getField("pak" + pakID).getInt((Object) null)));
                dis.skipBytes(offset);
            } else if (disPosition <= offset) {
                dis.skip((long) (offset - disPosition));
            }
            returnBytes = new byte[length];
            dis.readFully(returnBytes);
            disPosition = offset + length;
            } catch (IOException | NoSuchFieldException | IllegalAccessException e2) {
                e2.printStackTrace();
                }
        }
        return returnBytes;
    }

    public static byte[] getResourceFromJar(String s) {
        return getResourceFromJar(s, false);
    }

    public static byte[] getResourceFromJar(String s, boolean skipHeader) {
        try {
            ByteArrayOutputStream bout = new ByteArrayOutputStream();
            InputStream is = m_res.openRawResource(R.drawable.class.getField(s).getInt((Object) null));
            if (skipHeader) {
                is.skip(4);
            }
            while (true) {
                int read = is.read();
                if (read == -1) {
                    is.close();
                    return bout.toByteArray();
                }
                bout.write(read);
            }
        } catch (Exception e) {
            return null;
        }
    }

    public static void closeResource() {
        currentPak = -1;
        fileOffset = null;
        fileLength = null;
        try {
            if (dis != null) {
                dis.close();
                dis = null;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void initPak(int pakID) {
        if (currentPak != -1) {
            closeResource();
        }
        currentPak = pakID;
        try {
            dis = new DataInputStream(m_res.openRawResource(R.drawable.class.getField("pak" + pakID).getInt((Object) null)));
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            int numberOfFiles = dis.readShort();
            fileOffset = new int[numberOfFiles];
            fileLength = new int[numberOfFiles];
            for (int i = 0; i < numberOfFiles; i++) {
                dis.readInt();
                fileOffset[i] = dis.readInt();
                fileLength[i] = dis.readInt();
            }
            disPosition = (numberOfFiles * 12) + 2;
        } catch (Exception e2) {
        }
    }
}
